package it.giuseppetrisciuoglio.lyrics.parser;

import java.io.UnsupportedEncodingException;

import it.giuseppetrisciuoglio.lyrics.exception.LyricsNotFoundException;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.ParserException;

public class LyricsManiaTestoParser extends AbstractTestoParser {
	
	//http://www.lyricsmania.com/il_mio_canto_libero_lyrics_lucio_battisti.html
	private static String SITE = "http://www.lyricsmania.com/";
	private static String BLANK_CHAR = "_";

	@Override 
	public String getTesto(String httpResponse) throws ParserException, LyricsNotFoundException {
		NodeFilter filter =  new AndFilter(
		        new TagNameFilter("div"),
		        new HasAttributeFilter("id", "songlyrics_h"));
		return super.getTestoParse(httpResponse, filter);
	}

	@Override
	public String getURL(String artista, String titolo) throws IllegalArgumentException, UnsupportedEncodingException {
		super.validArtistaAndTitolo(artista, titolo);
		return SITE.concat(cleanTitolo(titolo.trim().toLowerCase().replaceAll(" ", BLANK_CHAR)))
				.concat(BLANK_CHAR).concat("lyrics")
				.concat(BLANK_CHAR).concat(cleanArtista(artista.trim().toLowerCase().replaceAll(" ", BLANK_CHAR)))
				.concat(".html");
	}
	
	@Override
	protected String cleanTitolo(String titolo) {
		titolo = super.cleanTitolo(titolo);
		return titolo.replace('\'', ' ')
				.replace('.', ' ')
				.replace('(', ' ')
				.replace(')', ' ')
				.replace(',', ' ')
				.replace('"', ' ')
				.replace(':', ' ')
				.replaceAll(" ", "");
	}

	@Override
	protected String cleanArtista(String artista) {
		artista = super.cleanArtista(artista);
		return artista.replace('\'', ' ')
				.replace('.', ' ')
				.replace('(', ' ')
				.replace(')', ' ')
				.replace(',', ' ')
				.replace('"', ' ')
				.replace(':', ' ')
				.replaceAll(" ", "");
	}
	
	

}
